package top.mataiji.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mataiji.manager.mapper.UserMapper;
import top.mataiji.manager.pojo.User;
import top.mataiji.manager.pojo.UserExample;
import top.mataiji.manager.service.UserService;

import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/14 - 03 - 14 - 22:16
 * @Description: top.mataiji.manager.service
 * @version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    //创建对象
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        //创建查询对象
        UserExample example = new UserExample();
        //设置查询参数
        example.createCriteria().andLoginnameEqualTo(user.getLoginname()).andPasswordEqualTo(user.getPassword());
        //查询，查询结果集为集合，获取集合的第一个参数
        System.out.println(user);
        return userMapper.login(user);
        /*if(!users.isEmpty()){
            return users.get(0);
        }else{
            return null;
        }*/
    }

    /**
     * 查询用户信息
     * @param userId
     * @return
     */
    @Override
    public User finduserById(Integer userId) {
        //创建查询对象
        UserExample example = new UserExample();
        //设置查询参数
        example.createCriteria().andUseridEqualTo(userId);
        return userMapper.selectByPrimaryKey(userId);
    }

    //注册
    @Override
    public Integer register(User user) {
        //
        return  0/*userMapper.insertSelective(user)*/;
    }

    @Override
    public Integer verifyName(String loginname) {
        int i = 0 ;
        //创建查询对象
        UserExample example = new UserExample();
        example.createCriteria().andLoginnameEqualTo(loginname);
        //查询
        List<User> users = userMapper.selectByExample(example);
        if(users!=null&&!users.isEmpty()){
            i = 1;
        }
        return i;
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @Override
    public int alterInformation(User user) {
        return userMapper.updateByPrimaryKey(user);
    }


}
